/**
 * Copyright:Copyright(c)2014-2015
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.services.manage.order.impl;

import shop.core.ServicesManager;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.cart.bean.Cart;
import shop.services.manage.cart.dao.CartDao;
import shop.services.manage.order.OrderService;
import shop.services.manage.order.bean.Order;
import shop.services.manage.order.dao.OrderDao;
import org.springframework.stereotype.Service;
import shop.services.manage.orderdetail.bean.Orderdetail;
import shop.services.manage.orderdetail.dao.OrderdetailDao;
import shop.services.manage.spec.bean.Spec;
import shop.services.manage.spec.dao.SpecDao;
import shop.services.manage.system.bean.User;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**   
 * @类名称：OrderServiceImpl      
 * @创建人：Ltz   
 * @创建时间：2017-09-20 下午15:48:44        
 * @版本号：1.0
 * @描述：     
 */
@Service("orderServiceManage")
public class OrderServiceImpl extends ServicesManager<Order, OrderDao> implements
OrderService {
    @Resource(name = "orderDaoManage")
    @Override
    public void setDao(OrderDao orderDao) {
    this.dao = orderDao;
    }
    @Resource(name = "specDaoManage")
    private SpecDao specDao;
    @Resource(name = "cartDaoManage")
    private CartDao cartDao;
    @Resource(name = "orderdetailDaoManage")
    private OrderdetailDao orderdetailDao;

    @Override
    public PagerModel selectMyOrder(Order order) {
        return dao.selectMyOrder(order);
    }

    @Override
    public PagerModel selectOrderPage(Order order) {
        return dao.selectOrderPage(order);
    }

    @Override
    public int insert(Order order) {
        dao.insert(order);
        if("order".equals(order.getPayType())){
            String[] productIDs = order.getProductIDs().split(",");
            String[] specIDs = order.getSpecIDs().split(";");
            String[] productAmount = order.getProductAmount().split(",");
            for (int i = 0; i < productIDs.length; i++) {
                //获取规格信息
                Spec spec = new Spec();
                spec.setProductID(productIDs[i]);
                spec.setCombination(specIDs[i]);
                spec = specDao.selectOne(spec);
                //创建订单详情项
                Orderdetail orderdetail = new Orderdetail();
                orderdetail.setOrderID(order.getId());
                orderdetail.setProductID(productIDs[i]);
                orderdetail.setPrice(spec.getPrice());
                orderdetail.setNumber(Integer.valueOf(productAmount[i]));
                orderdetail.setSpecInfo(spec.getCombination());
                //计算订单详情项价格
                BigDecimal productNumber = new BigDecimal(productAmount[i]);
                productNumber.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
                BigDecimal total = spec.getPrice().multiply(productNumber);
                orderdetail.setDtotal(total);
                orderdetailDao.insert(orderdetail);//插入订单详情
                //购物车删除已购买项
                if("cart".equals(order.getSubmitType())){
                    Cart cart = new Cart();
                    cart.setUserID(order.getUserID());
                    cart.setProductID(productIDs[i]);
                    if (cartDao.selectList(cart).size() != 0) {
                        cartDao.delete(cart);
                    }
                }
            }
        }
        return Integer.parseInt(order.getId());
    }
}

